<template>
  <div class="newProject">
    <div style="width: 95%;height: 838px; margin: auto;margin-top: 20px;">
      <div style="width: 100%;height: 46px;align-items: center;border-bottom: 2px solid #DCDFE6;margin-bottom: 30px; display: flex;
  align-items: center;  ">
        <span
          style="font-family: PingFangSC-Medium;font-weight: 500;font-size: 16px;color: #303133;line-height: 24px;">新建任务</span>
      </div>
      <div>
        <el-form ref="form" :model="form" :rules="rules">
          <el-row type="flex" justify="space-between" v-show="hasProject"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">是否有所属项目：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="radio">
                <el-radio-group v-model="radio" :disabled="canRadio">
                  <el-radio :label="1">是</el-radio>
                  <el-radio :label="2">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span>所属执行项目：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="">
                <el-select @change="projectChange" filterable :disabled="radio == 2" v-model="form.projectId"
                  placeholder="请输入所属执行项目" style="width: 100%; ">
                  <el-option v-for="item in projectOptions" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">任务名称：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="name">
                <el-input v-model="form.name" placeholder="请输入任务名称" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">任务类型：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="taskType">
                <el-select v-model="form.taskType" placeholder="请选择任务类型" style="width: 100%; ">
                  <el-option v-for="item in taskTypeOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">开始时间：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="releaseTime">
                <el-date-picker type="date" placeholder="选择日期" v-model="form.releaseTime" format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="">完成时间：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="endTime">
                <el-date-picker type="date" placeholder="选择日期" v-model="form.endTime" format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">任务下达性质：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="nature">
                <el-select v-model="form.nature" placeholder="请选择任务下达性质" style="width: 100%; ">
                  <el-option v-for="item in natureOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">承办部门：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="undertakeDeptId">
                <el-select v-model="form.undertakeDeptId" placeholder="请选择承办部门" style="width: 100%; ">
                  <el-option v-for="item in deptOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
                <!-- <el-cascader :options="deptOptions" :show-all-levels="false" v-model="form.undertakeDeptId"
                  placeholder="请选择承办部门" :props="{ checkStrictly: true }" style="width: 100%;" clearable></el-cascader> -->
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">任务量：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="taskCount">
                <el-input v-model="form.taskCount" placeholder="请输入任务量" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">任务单位：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="taskUnit">
                <el-select v-model="form.taskUnit" placeholder="请选择任务单位" style="width: 100%; ">
                  <el-option v-for="item in taskUnitOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">经费下达总额(万元)：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="feeTotal">
                <el-input oninput="value=value.replace(/[^\d]/g,'')" v-model="form.feeTotal" placeholder="请输入经费下达总额(万元)"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">经费下达单价(万元)：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="feeUnitPrice">
                <el-input oninput="value=value.replace(/[^\d]/g,'')" v-model="form.feeUnitPrice"
                  placeholder="请输入经费下达单价(万元)" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">任务编号：</span>
            </el-col>
            <el-col :span="3" class="colRight">
              <el-form-item label="">
                <el-input :disabled="true" style="height: 28px !important;" v-model="projectNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="3" class="colRight">
              <el-form-item label="" prop="taskNo">
                <!-- <el-input v-model="form.no" placeholder="请输入任务编号" style="height: 28px !important;"></el-input> -->
                <el-input v-model.number="taskNo" placeholder="请输入任务编号(正整数)" :precision="0" controls-position="right"
                  style="height: 28px !important;"
                  oninput="value = value.replace(/[^\d]/g, '');if(value.length>9); value=value.slice(0,9)"
                  :min="0"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required" v-if="rwfzr">任务负责人：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="responsiblePersonId" v-if="rwfzr">
                <el-select v-model="form.responsiblePersonId" placeholder="请选择任务负责人" style="width: 100%; ">
                  <el-option v-for="item in taskUnitOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>任务描述：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入任务描述" v-model="form.taskContent">
              </el-input>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>预期成果：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入预期成果" v-model="form.expecteAchievement">
              </el-input>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>备注：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入备注" v-model="form.remark">
              </el-input>
            </el-col>
          </el-row>
          <el-form-item style="margin-top: 30px;">
            <el-button type="primary" @click="onSubmit" style="background-color: #47ACF4;">保存</el-button>
            <el-button type="primary" @click="cancel"
              style="margin-left: 30px;background: transparent; color: #47ACF4;margin-right:24px ;">取消</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { newTask, getTaskNo } from '@/api/project/task.js'
import { getDeptList, getAllproject } from "@/api/project/project";
export default {
  components: {
  },
  data() {
    var validateData = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入完成时间'));
      } else if (this.form.releaseTime > value) {
        callback(new Error('完成时间必须大于开始时间'));
      } else {
        callback();
      }
    };
    var startData = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入开始时间'));
      } else if (value > this.form.endTime && this.form.endTime != '') {
        callback(new Error('开始时间必须小于完成时间'));
      } else {
        callback();
      }
    };
    return {
      isSubmit: false,
      zgb: false,
      rwfzr: false,
      canRadio: false,
      hasProject: true,
      radio: 1,
      form: {
        projectId: '',
        name: '',
        taskUnit: '',
        taskType: '',
        taskCount: '',
        releaseTime: '',
        endTime: '',
        nature: '',
        undertakeDeptId: '',
        feeTotal: '',
        feeUnitPrice: '',
        no: '',
        taskContent: '',
        expecteAchievement: '',
        remark: ''
      },
      userRole: [],
      projectOptions: [],
      taskTypeOptions: [],
      natureOptions: [],
      deptOptions: [],
      inputDisabled: true,
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
        ],
        taskType: [
          { required: true, message: '请选择任务类型', trigger: 'blur' },
        ],
        releaseTime: [
          { required: true, message: '请选择开始时间', trigger: 'blur' },
          { validator: startData, trigger: 'blur' },
        ],
        endTime: [
          { required: true, message: '请选择完成时间', trigger: 'blur' },
          { validator: validateData, trigger: 'blur' },
        ],
        nature: [
          { required: true, message: '请选择任务下达性质', trigger: 'blur' },
        ],
        undertakeDeptId: [
          { required: true, message: '请选择承办部门', trigger: 'blur' },
        ],
        taskCount: [
          { required: true, message: '请输入任务量', trigger: 'blur' },
        ],
        taskUnit: [
          { required: true, message: '请选择任务单位', trigger: 'blur' },
        ],
        feeTotal: [
          { required: true, message: '请输入经费下达总额', trigger: 'blur' },
        ],
        feeUnitPrice: [
          { required: true, message: '请输入经费下达单价', trigger: 'blur' },
        ],
        projectNo: [
          { required: true, message: '请输入任务编号', trigger: 'blur' },
        ],
      },
      taskUnitOptions: [],
      projectNo: '',
      taskNo: 1
    }
  },
  watch: {
    radio(newRadio) {
      if (newRadio == 2) {
        this.projectNo = 'temp'
        this.form.projectId = ''
      }
    }
  },
  mounted() {
    let proId = this.$route.query.id
    if (proId) {
      this.radio = 1
      this.canRadio = true
      this.form.projectId = proId
      getTaskNo(proId).then(res => {
        this.taskNo = res.data
        let arr = res.data.split("-");
        this.projectNo = arr[0]
        this.taskNo = parseInt(arr[1])
        console.log('this.taskNo', this.taskNo)
      })
    }
    this.userRole = this.$store.state.userInfo.userRoleKeyList
    console.log('this.userRole', this.userRole)
    this.userRole.forEach(item => {
      if (item == 'admin' || item == 'zgb_scgly') {
        this.zgb = true;
      }
      if (item == 'scbm_fzr' || item == 'admin') {
        this.rwfzr = true
      } else {
      }
    });
    getDeptList().then(res => {
      this.deptOptions = res.data[0].children
    });
    getAllproject().then(res => {
      this.projectOptions = res.data
    })
    this.natureOptions = this.getDict('xmgl_task_nature');
    this.taskTypeOptions = this.getDict('xmgl_task_type');
    this.taskUnitOptions = this.getDict('xmgl_task_unit');
  },
  methods: {
    projectChange() {
      let proId = this.form.projectId
      getTaskNo(proId).then(res => {
        this.taskNo = res.data
        let arr = res.data.split("-");
        this.projectNo = arr[0]
        this.taskNo = parseInt(arr[1])
      })
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.undertakeDeptId = this.form.undertakeDeptId[this.form.undertakeDeptId.length - 1]
          this.form.overallCompletionRate = 0;
          this.form.no = this.projectNo + '-' + this.taskNo
          newTask(this.form).then(res => {
            if (res.code == 200) {
              this.$modal.msgSuccess('新建任务成功！')
              this.isSubmit = true;
              if (this.zgb) {
                let proId = this.$route.query.id
                if (proId) {
                  this.$router.push('/projectDetails?id=' + proId + '&activeName=2');
                } else {
                  this.$router.push({
                    path: '/taskList',
                  });
                }
              }

            }
          })
        } else {
          this.$modal.msgError('请填写完完整信息！');
        }
      })

    },
    doDept() {
      if (this.isSubmit) {
        this.$modal.msgSuccess('已给相关部门发送任务！')
        let proId = this.$route.query.id
        if (proId) {
          this.$router.push('/projectDetails?id=' + proId + '&activeName=2');
        } else {
          this.$router.push({
            path: '/taskList',
          });
        }
      } else {
        this.$modal.msgError('请先保存！');
      }

    },
    cancel() {
      this.$router.go(-1);
    },
    getDict(dictName) {
      let dict = this.$store.state.dictNew[dictName]
      console.log(dict)
      return dict
    }
  }
}
</script>

<style scoped lang="scss">
.newProject {
  margin-left: 24px;
  margin-right: 24px;
  // width: 100%;
  height: 1000px;
  background-color: #FFFFFF;
  margin-top: 20px;

  .colRight {

    padding: 10px;
    background-color: #FFFFFF;
  }

  .colLeft {
    /* border: 1px solid #DCDFE6; */
    background-color: #F5F7FA;
    text-align: center;
    margin: auto;
  }
}

span.required::after {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>
<style>
.el-form-item__error {
  z-index: 999 !important;
}
</style>
